import React from 'react'
import {Input ,Radio ,Select ,Button  ,message} from 'antd';
import './index.less';
import axios from 'axios';
import {PATH,IP} from '../../../../../../utils/urls';
const { Option } = Select;
import BreadcrumbCustom from "../../../../../../components/BreadcrumbCustom";
export default class EidtResourceGegatherConf extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            resouceType: 0,
            appId:"",
            name:"",
            resouceName:"",
            md5Key:"",
            frequency:"",
            resouceIp:"",
            interfaceBandwidth:"",
            macAddress:"",
            snmpVersion:"",
            orgName:"",
            hardWareArray:[]
        };
    }

    componentDidMount() {
        console.log(this.props);
        if(this.props.location.state == null){
            return;
        }
        let monitorTypeDictId = "";
        if(this.props.location.state.data.monitorTypeDictId){
            monitorTypeDictId = this.props.location.state.data.monitorTypeDictId;
            this.getShuJuXiangDict(monitorTypeDictId)
        }
        this.setState({
            appId:this.props.location.state.appId,
            name:this.props.location.state.name,
            monitorTypeDictId:monitorTypeDictId
        })

        let equipMentId = localStorage.getItem("softIdCurrent");
        if(equipMentId && equipMentId!=""){
            this.resourceGatherConf(equipMentId);
        }
    }

    /**
     * 获取数据项列表
     * @returns
     */
    resourceGatherConf(id) {
        axios.post(PATH.resourceGatherConf, {
            isSoftware:0,
            resourceId:id
        })
            .then(response => {
                console.log(response);
                if (response.data.code == 0) {
                    let dataArray = response.data.data;
                    console.log(response.data.data);
                    console.log(dataArray);
                    this.setState({
                        hardWareArray:dataArray
                    })
                }
            });
    }

    /**
     * 获取数据项字典
     * @param e
     */
    getShuJuXiangDict(monitorTypeDictId){
        axios.post(PATH.getDict,{

        })
    }

    onChange = e =>{
        console.log(e.target.value);
        this.setState({
            value: e.target.value,
        });
    }

    handleChange = e=> {
        console.log(e);
    }
    /**
     * 返回
     * @returns {*}
     */
    backFun=()=>{
        let path={
            pathname:"equipmentparamssetup"
        }
        this.props.history.push(path);
    }

    /**
     * 输入框值变化
     * @returns {*}
     */
    inputChange = (event)=>{
        console.log(123);
        if(event && event.target && event.target.value){
            let value = event.target.value;
            switch (event.target.id) {
                case "editEquipMentInput0":
                    this.setState({appId:value});
                    break;
                case "editEquipMentInput1":
                    this.setState({name:value});
                    break;
            }
        }
    }

    /**
     * 保存
     * @returns {*}
     */
    onSubmit=()=>{
        console.log(this.state);
        axios.post(PATH.addHardWareResource,{
            name:this.state.name,
            resouceType:this.state.resouceType,
            md5Key:this.state.md5Key,
            frequency:this.state.frequency,
            resouceIp:this.state.resouceIp,
            interfaceBandwidth:this.state.interfaceBandwidth,
            macAddress:this.state.macAddress,
            snmpVersion:this.state.snmpVersion,
            orgName:this.state.orgName,
        }).then(response=>{
            console.log(response);
            if(response.data.code == 0){
                message.success(response.data.msg);
            }else{
                message.error(response.data.msg);
            }
        })
    }

    render() {
        console.log(this.state.hardWareArray);
        return (
            <div>
                <BreadcrumbCustom first="资源配置" second="管理设备参数" />
                <div className="changeAllContent">
                    <Button onClick={this.backFun} className="backBtn">返回</Button>
                    <div className="resourceContentCell">
                        <div className="changeTitle">数据项:</div>
                        <Select className="changeCell" onChange={this.handleChange}>
                            {
                                this.state.hardWareArray.map((item,index)=>(
                                    <Option value={index} key={index}>{item.monitorTypeDictName}</Option>
                                ))
                            }
                        </Select>
                    </div>
                    <div className="resourceContentCell">
                        <div className="changeTitle">OID:</div>
                        <Input onChange={event=>this.inputChange(event)}  id="editEquipMentInput0" className="changeCell" value={this.state.appId} placeholder="请输入OID"/>
                    </div>
                    <div className="resourceContentCell">
                        <div className="changeTitle">描述:</div>
                        <Input onChange={event=>this.inputChange(event)}  id="editEquipMentInput1" className="changeCell" value={this.state.name} placeholder="请输入描述" />
                    </div>
                    <div className="resourceContentCell">
                        <div className="changeTitle">请求类型:</div>
                        <Radio.Group className="changeCell changeRaio" onChange={this.onChange}>
                            <Radio value={0}>GET</Radio>
                            <Radio value={1}>WALK</Radio>
                        </Radio.Group>
                    </div>
                    <div className="resourceContentCell changeHardBtnDiv">
                        <Button className="changeHardBtn" onClick={this.onSubmit}>保存</Button>
                    </div>
                </div>
            </div>
        )
    }
}